package tt.dz.biz.service.rpc;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import java.util.List;
import javax.annotation.Resource;
import org.apache.avro.Protocol;
import org.apache.avro.Protocol.Message;
import org.apache.avro.generic.GenericRecord;
import org.apache.commons.collections.CollectionUtils;
import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Service;
import tt.dz.biz.dao.user.OperListDAO;
import tt.dz.biz.dao.user.OperRoleDAO;
import tt.dz.biz.dao.user.RoleDAO;
import tt.dz.common.DzDigester;
import tt.dz.vo.OperListVO;
import tt.dz.vo.OperRoleVO;
import tt.dz.vo.response.OperLoginRes;
import tt.dz.vo.response.OperLoginRes.ErrorMsg;
import tt.dz.vo.response.OperLoginRes.Role;

@Service("rpcOperLoginService")
public class RPCOperLoginService
  implements RPCBaseService
{
  private static final Logger logger = LoggerFactory.getLogger(RPCOperLoginService.class);
  @Resource(name="operListDAO")
  private OperListDAO operListDao;
  @Resource(name="operRoleDAO")
  private OperRoleDAO operRoleDAO;
  @Resource(name="roleDAO")
  private RoleDAO roleDAO;
  
  public Object execute(Protocol.Message message, Object request, Protocol protocol)
    throws Exception
  {
    OperLoginRes result = new OperLoginRes();
    result.setIsSuccess(Integer.valueOf(0));
    
    Object req = ((GenericRecord)request).get("req");
    
    JSONObject jsonObj = JSON.parseObject(req.toString());
    String tmpMobile = jsonObj.getString("mobileNo");
    String tmpPwd = jsonObj.getString("password");
    String tmpSysCode = jsonObj.getString("sysCode");
    if ((StringUtils.isBlank(tmpMobile)) || (StringUtils.isBlank(tmpPwd)) || (StringUtils.isBlank(tmpSysCode))) {
      return JSON.toJSONString(result);
    }
    List<OperListVO> operList = this.operListDao.queryOperForLogin(tmpMobile);
    if (CollectionUtils.isEmpty(operList))
    {
      result.setErrorMsg(OperLoginRes.ErrorMsg.NOT_FOUND);
    }
    else if ((operList.size() == 1) && (((OperListVO)operList.get(0)).getIsLocked() != null) && 
      (((OperListVO)operList.get(0)).getIsLocked().intValue() == 1))
    {
      result.setErrorMsg(OperLoginRes.ErrorMsg.LOCKED);
    }
    else
    {
      boolean isSuccess = false;
      OperListVO oper = new OperListVO();
      for (OperListVO operListVO : operList) {
        if (StringUtils.equals(operListVO.getPassword(), DzDigester.sha1ForOper(tmpPwd)))
        {
          isSuccess = true;
          oper = operListVO;
          break;
        }
      }
      if (!isSuccess)
      {
        result.setErrorMsg(OperLoginRes.ErrorMsg.BAD_PWD);
        return JSON.toJSONString(result);
      }
      result.setIsSuccess(Integer.valueOf(1));
      result.setUserId(oper.getManagerId());
      result.setUserName(oper.getName());
      
      OperRoleVO param = new OperRoleVO();
      param.setManagerId(oper.getManagerId());
      param.setSysCode(tmpSysCode);
      
      List<OperRoleVO> roleList = this.operRoleDAO.getOperRoleByOper(param);
      for (OperRoleVO o : roleList)
      {
        OperLoginRes tmp382_380 = result;tmp382_380.getClass();
        OperLoginRes.Role r = new OperLoginRes.Role();
        try
        {
          r.setRoleId(o.getRoleId());
          r.setRoleName(o.getRoleName());
        }
        catch (Exception e)
        {
          logger.error("", e);
        }
        result.addRole(r);
      }
    }
    return JSON.toJSONString(result);
  }
}
